package com.cgsd.heyu.ui.task.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.android.library.image.GlideUtil;
import com.android.library.utils.DpPxUtil;
import com.android.library.utils.LogUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.ui.task.vo.TempStepTwoBean;
import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.GsonBuilder;

/**
 * Author: sky on 2021/3/4 16:42
 * Email: xcode126@126.com
 * Desc:
 */
public class StepTwoAdapter extends BaseItemDraggableAdapter<TempStepTwoBean.StepListBean, BaseViewHolder> {

    public String video;

    public StepTwoAdapter() {
        super(R.layout.item_step_two, null);
    }

    @Override
    protected void convert(BaseViewHolder helper, TempStepTwoBean.StepListBean item) {
        helper.addOnClickListener(R.id.ll_no)
                .addOnClickListener(R.id.tv_delete)
                .addOnClickListener(R.id.ll_picture)
                .addOnClickListener(R.id.ll_text)
                .addOnClickListener(R.id.iv_close)
                .addOnClickListener(R.id.iv_example);

        helper.setText(R.id.et_require, item.title);
        helper.setText(R.id.tv_step, "" + item.step).setText(R.id.tv_step_title, "步骤" + item.step);
        //处理输入框
        handleInput(helper, item);
        //处理图片
        if (TextUtils.isEmpty(item.image)) {
            helper.setImageResource(R.id.iv_example, R.mipmap.ic_add_picture);
            helper.setGone(R.id.iv_close, false);
        } else {
            GlideUtil.loadCircularAllImage(mContext, item.image, helper.getView(R.id.iv_example), DpPxUtil.dip2px(mContext, 8));
            helper.setGone(R.id.iv_close, true);
        }
        //处理选择状态
        handleState(helper, item);
    }

    /**
     * 处理输入框
     *
     * @param helper
     * @param item
     */
    private void handleInput(BaseViewHolder helper, TempStepTwoBean.StepListBean item) {
        EditText etRequire = helper.getView(R.id.et_require);
        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtils.e("textWatcher", helper.getAdapterPosition() + "");
                if (etRequire.hasFocus()) {//判断当前EditText是否有焦点在
                    String trim = etRequire.getText().toString().trim();
                    item.title = trim;
                }
            }
        };
        //设置EditText的焦点监听器判断焦点变化，当有焦点时addTextChangedListener，失去焦点时removeTextChangedListener
        etRequire.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                etRequire.addTextChangedListener(textWatcher);
                LogUtils.e("addTextChanged", helper.getAdapterPosition() + "");
            } else {
                etRequire.removeTextChangedListener(textWatcher);
                LogUtils.e("removeTextChanged", helper.getAdapterPosition() + "");
            }
        });
    }

    /**
     * 处理反馈状态
     *
     * @param helper
     * @param item
     */
    private void handleState(BaseViewHolder helper, TempStepTwoBean.StepListBean item) {
        helper.setGone(R.id.fl_example, true);
        if (item.backcom == 0 && item.backimg == 0) {//图片和文本都不反馈
//            helper.setGone(R.id.fl_example, false);
            helper.getView(R.id.ll_no).setSelected(true);
            helper.getView(R.id.ll_picture).setSelected(false);
            helper.getView(R.id.ll_text).setSelected(false);
        } else {
            if (item.backimg == 1) {//反馈图片
//                helper.setGone(R.id.fl_example, true);
                helper.getView(R.id.ll_no).setSelected(false);
                helper.getView(R.id.ll_picture).setSelected(true);
                helper.getView(R.id.ll_text).setSelected(false);
            } else {//反馈文字
//                helper.setGone(R.id.fl_example, false);
                helper.getView(R.id.ll_no).setSelected(false);
                helper.getView(R.id.ll_picture).setSelected(false);
                helper.getView(R.id.ll_text).setSelected(true);
            }
        }
    }

    /**
     * 检测输入
     *
     * @return
     */
    public String checkInput() {
        if (mData != null && mData.size() > 0) {
            for (TempStepTwoBean.StepListBean stepBean : mData) {
//                TempStepTwoBean.StepListBean stepBean = mData.get(i);
                //有标题未填写的
                if (TextUtils.isEmpty(stepBean.title)) {
                    return "请输入第" + stepBean.step + "步的标题";
                }
                //有选了图片，没添加示例图的
                if (stepBean.backimg == 1 && TextUtils.isEmpty(stepBean.image)) {
                    return "请添加第" + stepBean.step + "步的示例图片";
                }
            }
            return "";//通过检测
        } else {
            return "配置错误或出现异常，请联系管理员！";
        }
    }

    /**
     * 获取保存参数
     *
     * @return
     */
    public String getSaveParam() {
        TempStepTwoBean twoBean = new TempStepTwoBean(video);
        twoBean.steplist = mData;
        //保持值为null的字段不会抹除
        return new GsonBuilder().serializeNulls().create().toJson(twoBean);
    }

    public void updateData() {
        if (mData != null && mData.size() > 0) {
            for (int i = 0; i < mData.size(); i++) {
                TempStepTwoBean.StepListBean bean = mData.get(i);
                bean.step = i + 1;
            }
        }
        notifyDataSetChanged();
    }

}
